package calendar;

import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import network.e2e.FrameReader;
import network.e2e.FrameWriter;

public class CalendarParser {
  
  /**
   * Decode a calendar from the network
   * @requires reader not null
   * @param reader the net
   * @return the calendar read
   * @throws IOException 
   */
  public static Calendar retrieve(FrameReader reader) throws IOException {
    assert (reader != null);
    Integer year,month,day,hour,min,sec;
    year = reader.readInt();
    month = reader.readInt();
    day = reader.readInt();
    hour = reader.readInt();
    min = reader.readInt();
    sec = reader.readInt();
    assert (year != null && month != null && day != null && hour != null
        && min != null && sec != null);
    return new GregorianCalendar(year.intValue(), month.intValue(), day.intValue(), hour.intValue(), min.intValue(), sec.intValue());
  }
  
  /**
   * Encode a calendar to be sent over the network
   * @requires param not null
   * @param calendar the calendar
   * @param writer  the net
   * @throws IOException 
   */
  public static void send(Calendar calendar, FrameWriter writer) throws IOException {
    assert calendar != null && writer != null;
    writer.writeInt(calendar.get(Calendar.YEAR));
    writer.writeInt(calendar.get(Calendar.MONTH));
    writer.writeInt(calendar.get(Calendar.DAY_OF_MONTH));
    writer.writeInt(calendar.get(Calendar.HOUR_OF_DAY));
    writer.writeInt(calendar.get(Calendar.MINUTE));
    writer.writeInt(calendar.get(Calendar.SECOND));
  }
  
}
